package com.hc.pokemen.feature_pokemen.presentation.pokemon_detail

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import coil.compose.AsyncImage
import com.hc.pokemen.feature_pokemen.domain.entity.Pokemon
import com.hc.pokemen.feature_pokemen.domain.entity.Stat
import com.hc.pokemen.feature_pokemen.domain.entity.Type
import com.hc.pokemen.feature_pokemen.presentation.pokemon_detail.components.PokemonBaseStats
import com.hc.pokemen.feature_pokemen.presentation.pokemon_detail.components.PokemonDetailTopSection
import com.hc.pokemen.feature_pokemen.presentation.pokemon_detail.components.PokemonTypeSection
import com.hc.pokemen.feature_pokemen.presentation.pokemon_detail.components.PokemonWhSection

@Preview
@Composable
fun PokemonDetailScreenPreview() {

    PokemonDetailScreen(
        PokemonDetailState(
            isLoading = true,
            pokemon = Pokemon(
                1,
                "Pidgeot",
                imageUrl = "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/1.png",
                weight = 39.5f,
                height = 1.5f,
                types = listOf(Type("Normal"), Type("Flying")),
                stats = listOf(
                    Stat(name = "hp", baseStat = 83),
                    Stat(name = "attack", baseStat = 50),
                    Stat(name = "attack", baseStat = 75),
                    Stat(name = "attack", baseStat = 70),
                    Stat(name = "attack", baseStat = 101)
                )
            )
        ),
        dominantColor = Color.White,
    ) {

    }
}

@Composable
fun PokemonDetailScreen(
    controller: NavController,
    vm: PokemonDetailViewModel = hiltViewModel(),
    dominantColor: Color,
    pokemonName: String,
) {
    LaunchedEffect(Unit) {
        vm.getPokemonInfo(pokemonName)
        vm.effect.collect {
            when (it) {
                is PokemonDetailEffect.BackPokemonList -> {
                    controller.navigateUp()
                }
            }
        }
    }
    PokemonDetailScreen(vm.state, dominantColor = dominantColor) {
        vm.onEvent(it)
    }
}

@Composable
fun PokemonDetailScreen(
    state: PokemonDetailState,
    dominantColor: Color,
    topPadding: Dp = 20.dp,
    pokemonImageSize: Dp = 200.dp,
    onEvent: (event: PokemonDetailEvent) -> Unit,
) {
    Box(
        Modifier
            .fillMaxSize()
            .background(dominantColor)
            .statusBarsPadding()
            .padding(bottom = 16.dp)
    ) {
        //标题
        PokemonDetailTopSection(
            Modifier
                .fillMaxWidth()
                .fillMaxHeight(0.2f)
                .clickable {
                    onEvent(PokemonDetailEvent.BackClick)
                }
        )

        //详情
        state.pokemon?.let { pokemon ->
            //内容区域
            val scrollState = rememberScrollState()
            Column(
                horizontalAlignment = Alignment.CenterHorizontally,
                modifier = Modifier
                    .padding(
                        top = topPadding + pokemonImageSize / 2f,
                        start = 16.dp,
                        end = 16.dp,
                        bottom = 16.dp
                    )
                    .shadow(10.dp, RoundedCornerShape(10.dp))
                    .clip(RoundedCornerShape(10.dp))
                    .background(MaterialTheme.colorScheme.surface)
                    .padding(16.dp)
                    .fillMaxSize()
                    .offset(y = 100.dp)
                    .verticalScroll(scrollState)
            ) {
                Text(text = "#${pokemon.id} ${pokemon.name}")
                PokemonTypeSection(types = pokemon.types)
                PokemonWhSection(weight = pokemon.weight, pokemon.height)
                PokemonBaseStats(pokemon.stats)
            }

            //图片
            AsyncImage(
                modifier = Modifier
                    .align(Alignment.TopCenter)
                    .size(pokemonImageSize)
                    .offset(y = topPadding),
                model = pokemon.imageUrl,
                contentDescription = null
            )
        }
        //加载
        if (state.isLoading) {
            CircularProgressIndicator(
                color = Color.Blue,
                modifier = Modifier
                    .size(100.dp)
                    .align(Alignment.Center)
                    .padding(top = topPadding + pokemonImageSize / 2)
            )
        }

    }
}